﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using Newtonsoft.Json;

using Gx.Model.Role;//实体
using Gx.Model.Other;
using Gx.Model.Enums;
using Gx.Dal.Role;
using Gx.Common;
using Gx.Model;

namespace Gx.Bll.Role
{
    public class M_RolePageUrlInfoBll
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public string GetRolePageUrljson()
        {
            Menus meuns = new Menus()
            {
                menus = new List<MenusList>(),
            };
            int empId = LoginHelper.GetUserId();
            if (empId == 0)
            {
                return "{\"menus\":[]}";
            }
            int roleId = LoginHelper.GetRoleId();
            List<M_PageUrlInfo> pageList = new M_PageUrlInfoBll().GetMenuInfoByRoleId(roleId);
            List<M_PageUrlInfo> roleOne = pageList.Where(x => x.LevelID == ((int)PageLevel.一级).ToString()).ToList();
            List<M_PageUrlInfo> roleTwo = null;
            for (int i = 0; i < roleOne.Count; i++)
            {
                meuns.menus.Add(new MenusList()
                {
                    menuid = roleOne[i].ID,
                    icon = roleOne[i].Icon,
                    menuname = roleOne[i].UrlName,
                    menus = new List<MenusList>(),
                });
                if (pageList.Exists(x => x.LevelID == roleOne[i].ID))
                {
                    roleTwo = pageList.Where(x => x.LevelID == roleOne[i].ID).ToList();
                    for (int ii = 0; ii < roleTwo.Count; ii++)
                    {
                        meuns.menus[i].menus.Add(new MenusList()
                        {
                            menuid = roleTwo[ii].ID,
                            icon = roleTwo[ii].Icon,
                            menuname = roleTwo[ii].UrlName,
                            url = roleTwo[ii].Url
                        });
                    }
                }
            }
            return JsonHelper.SerializeObject(meuns);
        }

        public bool DelRolePageUrlInfo(int ID)
        {
            using (M_RolePageUrlInfoDal dal = new M_RolePageUrlInfoDal())
            {
                return dal.DelRolePageUrlInfo(ID);
            }
        }

        public List<M_RolePageUrlInfo> GetRolePageUrlInfoList(int? roleId)
        {
            if (roleId == null || roleId < 1)
            {
                return new List<M_RolePageUrlInfo>();
            }
            using (M_RolePageUrlInfoDal dal = new M_RolePageUrlInfoDal())
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" AND RoleID={0}", roleId);

                return dal.GetRolePageUrlInfoList(strWhere.ToString());
            }
        }

        public List<M_RolePageUrlInfo> GetRolePageUrlInfoList(string strWhere)
        {
            using (M_RolePageUrlInfoDal dal = new M_RolePageUrlInfoDal())
            {
                return dal.GetRolePageUrlInfoList(strWhere);
            }
        }
        /// <summary>
        /// 添加权限(事务操作)
        /// </summary>
        /// <returns></returns>
        public ReturnModel InsertRolePageUrlInfo(int roleId, string pageId)
        {
            ReturnModel r = new ReturnModel()
            {
                Msg = "操作失败",
                Success = false,
            };
            DataTable dt = new DataTable();
            dt.Columns.Add("RoleID");
            dt.Columns.Add("PageUrlID");
            List<string> array = pageId.split(',').Distinct().ToList();
            foreach (string item in array)
            {
                DataRow dr = dt.NewRow();
                dr["RoleID"] = roleId;
                dr["PageUrlID"] = item;
                dt.Rows.Add(dr);
            }
            using (M_RolePageUrlInfoDal dal = new M_RolePageUrlInfoDal())
            {
                dal.DelRolePageUrlInfo(roleId);
                bool res = dal.InsertRolePageUrlInfo(dt);
                if (res)
                {
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }
    }
}